/**
 * 
 */
package com.cchr.acms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.cchr.acms.constant.CaseStatus;
import com.cchr.acms.dao.CaseInfoDAO;
import com.cchr.acms.model.CaseAudit;
import com.cchr.acms.model.CaseInfo;
import com.cchr.acms.model.CaseInfoQueryEntity;
import com.cchr.acms.service.CaseAuditService;
import com.cchr.acms.service.CaseInfoService;

/**
 * @author Zeng
 *
 */
@Service
public class CaseInfoServiceImpl implements CaseInfoService{
 
    @Autowired
    private CaseInfoDAO caseInfoDAO;
	@Autowired
	private CaseAuditService caseAuditService;
     
    public int insertCaseInfo(CaseInfo caseInfo) {
        int result = caseInfoDAO.insertCaseInfo(caseInfo);
        return result;
    }
    
    public int updateCaseInfo(CaseInfo caseInfo){
    	int result = caseInfoDAO.updateCaseInfo(caseInfo);
    	return result;
    }
    
    public CaseInfo getCaseInfoById(int id){
    	return caseInfoDAO.getCaseInfoById(id);
    }

	public List<CaseInfo> getAllCaseInfo() {
		return caseInfoDAO.getAllCaseInfo();
	}

	
	@SuppressWarnings("unchecked")
	public List<CaseInfo> queryAbuseInfo(CaseInfoQueryEntity entity, int recordIndex, int pageNum) {
		Map paramMap=new HashMap();
		if(entity != null)
		{
			paramMap.put("patientName", entity.getPatientName().trim());
		    paramMap.put("patientLastName", entity.getPatientLastName().trim());
			paramMap.put("doctorName", entity.getDoctorName().trim());
			paramMap.put("doctorLastName", entity.getDoctorLastName().trim());
			paramMap.put("facilityName", entity.getFacilityName().trim());
			paramMap.put("drugName", entity.getDrugName().trim());
			paramMap.put("abuseStart", entity.getAbuseStart());
			paramMap.put("abuseEnd", entity.getAbuseEnd());
			paramMap.put("abusedAge", entity.getAbusedAge());
			paramMap.put("abusedcity", entity.getAbusedcity().trim());
			paramMap.put("abusedstate", entity.getAbusedstate().trim());
			paramMap.put("abusedcountry", entity.getAbusedcountry().trim());
			paramMap.put("drugType", entity.getDrugType().trim());
			paramMap.put("abuseType", entity.getAbuseType().trim());
			paramMap.put("doctorType", entity.getDoctorType().trim());
			paramMap.put("facilityType", entity.getFacilityType().trim());
			paramMap.put("facilityCity", entity.getFacilityCity().trim());
			paramMap.put("facilityState", entity.getFacilityState().trim());
			paramMap.put("facilityCountry", entity.getFacilityCountry().trim());
			if(!StringUtils.isEmpty(entity.getHasAttoney().trim()))
			{
				paramMap.put("hasAttoney", entity.getHasAttoney().trim());
			}
			paramMap.put("insuranceType", entity.getInsuranceType().trim());
		}
		
		paramMap.put("recordIndex", recordIndex);
		paramMap.put("pageNum", pageNum);
		paramMap.put("userId",entity.getUserId());
		return caseInfoDAO.getPagedCaseInfo(paramMap);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int queryTotal(CaseInfoQueryEntity entity) {
		Map paramMap=new HashMap();
		if(entity != null)
		{
			paramMap.put("patientName", entity.getPatientName().trim());
			paramMap.put("patientLastName", entity.getPatientLastName().trim());
			paramMap.put("doctorName", entity.getDoctorName().trim());
			paramMap.put("doctorLastName", entity.getDoctorLastName().trim());
			paramMap.put("facilityName", entity.getFacilityName().trim());
			paramMap.put("drugName", entity.getDrugName().trim());
			paramMap.put("abuseStart", entity.getAbuseStart());
			paramMap.put("abuseEnd", entity.getAbuseEnd());
			paramMap.put("abusedAge", entity.getAbusedAge());
			paramMap.put("abusedcity", entity.getAbusedcity().trim());
			paramMap.put("abusedstate", entity.getAbusedstate());
			paramMap.put("abusedcountry", entity.getAbusedcountry().trim());
			paramMap.put("drugType", entity.getDrugType().trim());
			paramMap.put("abuseType", entity.getAbuseType().trim());
			paramMap.put("doctorType", entity.getDoctorType().trim());
			paramMap.put("facilityType", entity.getFacilityType().trim());
			paramMap.put("facilityCity", entity.getFacilityCity().trim());
			paramMap.put("facilityState", entity.getFacilityState().trim());
			paramMap.put("facilityCountry", entity.getFacilityCountry().trim());
			if(!StringUtils.isEmpty(entity.getHasAttoney().trim()))
			{
				paramMap.put("hasAttoney", entity.getHasAttoney().trim());
			}
			paramMap.put("insuranceType", entity.getInsuranceType().trim());
			paramMap.put("userId", entity.getUserId());
		}
		
		return caseInfoDAO.queryTotal(paramMap);
	}

	public int updateCaseStatus(int id,String status, String lastUpdatedBy) {
		Map paramMap=new HashMap();
		paramMap.put("status", status);
		paramMap.put("id", id);
		paramMap.put("lastUpdatedBy", lastUpdatedBy);
		
		//录入change history
		
		String oldStatus ;
		oldStatus = CaseStatus.getKeyByValue(caseInfoDAO.getCaseInfoById(id).getStatus());
		String newStatus;
		newStatus = CaseStatus.getKeyByValue(status);
		CaseAudit audit = new CaseAudit();
		audit.setAction("Update");
		audit.setUpdatedTime(new Date());
		audit.setCaseId(id);
		audit.setUpdatedBy(lastUpdatedBy);
		StringBuilder updateContent = new StringBuilder();
		updateContent.append("\"Case Status\" " ).append(" has changed from ")  
		.append("\"").append(oldStatus).append("\"").append(" to ").append("\"").append(newStatus).append("\"")
		.append("<br>");
		audit.setUpdatedAfter(updateContent.toString());
		caseAuditService.insertCaseAudit(audit);
		
    	int result = caseInfoDAO.updateCaseStatus(paramMap);
    	return result;
	}

	@Override
	public int updateUserToCase(Integer userId, Integer caseId, String notes) {
		Map paramMap=new HashMap();
		paramMap.put("userId", userId);
		paramMap.put("id", caseId);
		paramMap.put("notes", notes);
    	return caseInfoDAO.updateUserToCase(paramMap);
		
	}
}
